<?
$title = "Edit category";
$pageid = "5";
$accessid = "a00";

require("required/dbconnect.php");
require("required/function.php");

if (isset($_POST['btnsubmit'])) {
    $country_id = $_POST['country_id'];
    $zone_id = $_POST['zone_id'];
    $courier_id = $_POST['courier_id'];

    $chk = mysql_query("select count(*) as result from os_shipping_zone_country where country_id=$country_id and zone_id=$zone_id");

    if (mysql_result($chk, 0, "result") == 0) {
        mysql_query("insert into os_shipping_zone_country(country_id,zone_id,courier_id)values('$country_id','$zone_id','$courier_id')") or die(mysql_error());
        header("Location:shipping_zone_country.php?id=$courier_id&action=2");
    } else {
        header("Location:shipping_zone_country.php?id=$courier_id&action=3");
    }
}

if (isset($_GET['zone_id']) && $_GET['zone_id'] != "") {
    $zone_id = $_GET['zone_id'];
    $zone_q = " and zone_id='$zone_id'";
    $zone_q_2 = " and a.id='$zone_id'";
} else {
    $zone_id = "";
    $zone_q = "";
    $zone_q_2 = "";
}

require("required/header.php");

checkAccess("allow_shipping_management", 1);

$id = $_GET['id'];

$get_info = mysql_query("select * from os_shipping_company where id='$id'");

if (mysql_num_rows($get_info) == 0) {
    echo "No record found!!";
} else {


    $a = mysql_fetch_array($get_info, MYSQL_ASSOC);
    if ($a['status'] == 0) {
        $slt0 = "selected";
        $slt1 = "";
    } else {
        $slt0 = "";
        $slt1 = "selected";
    }
    ?>
    <script src="required/js/jquery-1.4.2.js"></script>
    <script src="required/js/common_javascript.js"></script>
    <?
    if (!empty($_GET['action'])) {
        $updated = $_GET['action'];

        switch ($updated) {
            case 1:
                $msg = "<div class='dw_success'>Record Updated</div>";
                break;
            case 2:
                $msg = "<div class='dw_success'>Country Added</div>";
                break;
            case 3:
                $msg = "<div class='dw_failed'>Country previously added for this zone</div>";
                break;
            case 4:
                $msg = "<div class='dw_success'>Country Removed</div>";
                break;
            default:
                $msg = "unknown";
                break;
        }
        echo "<script>displayMsg('divMsg')</script>";
    } else {
        $msg = "";
    }
    ?>

    <table width="95%" cellpadding="2" cellspacing="1">
        <td valign="top">
            <table width="700">
                <tr>
                    <td colspan="4"><font class="title">Shipping Company</font><br />
                        <div id="divMsg"><?= $msg ?></div>
                    </td>
                </tr>
                <input type="hidden" value="<?= $id ?>" name="id">
                <tr>
                    <td width="100">Company Name</td>
                    <td width="5">:</td>
                    <td><?= $a['company_name'] ?></td>
                </tr>


                <tr>
                    <td colspan="3" align="center"><br>


                        <input type="button" value="Back" class="button" onclick="window.open('shipping_view_full.php?id=<?= $id ?>','_self')">
                        <br>
                        <br></td>
                </tr>
                <tr>
                    <td colspan="3">
                        <form action="<?= $_SERVER['PHP_SELF'] ?>" method="get">
                            <table width="100%">
                                <Td>
                                    <div class="sub_title">Shipping Country</div>
                                </td>
                                <td align="right">
                                    Zone
                                    <select name="zone_id">
                                        <option value="">All</option>
                                        <?
                                        $get_zone = mysql_query("select * from os_shipping_zone where courier_id='$id' order by zone_name asc") or die(mysql_error());

                                        while ($c = mysql_fetch_assoc($get_zone)) {

                                            $slt = $zone_id == $c['id'] ? "selected" : "";
                                            ?>
                                            <option value="<?= $c['id'] ?>" <?= $slt ?>><?= $c['zone_name'] ?></option>
                                        <? } ?>
                                    </select>
                                    <input type="hidden" value="<?= $id ?>" name="id">
                                    <input type="submit" value="Sumit" name="btnsubmit">
                                </td>

                            </table>
                        </form>
                        <br />

                        <table width="100%" class="solidborder">
                            <tr class="table_header">
                                <td>No.</td><td>Zone</td><td>Country</td><td>Action</td>
                            </tr>
                            <?
                            $get_country = mysql_query("select a.zone_name,a.id as aid,a.courier_id,b.id as bid,b.zone_id,country_id,c.id as cid,c.engname 
                                                        from os_shipping_zone a inner join os_shipping_zone_country b on a.id=b.zone_id inner join os_country c on 
                                                        b.country_id=c.id where a.courier_id='$id' $zone_q_2 order by a.zone_name asc,c.engname asc") or die(mysql_error());

                            if (mysql_num_rows($get_country) == 0) {
                                ?>
                                <tr>
                                    <Td colspan="6" align="center"> - No Record -</Td>
                                </tr>
                                <?
                            } else {
                                $no = 1;

                                while ($r = mysql_fetch_array($get_country, MYSQL_ASSOC)) {
                                    if ($no % 2 == 0) {
                                        $class1 = "table_item_even";
                                        $class2 = "table_item_odd";
                                    } else {
                                        $class1 = "table_item_odd";
                                        $class2 = "table_item_even";
                                    }
                                    ?>	
                                    <tr height="20" valign="top" class="<?= $class1 ?>"  onmouseover="this.className='table_item_mouseover'" 
                                        onmouseout="this.className='<?= $class1 ?>'">
                                        <Td><?= $no ?></Td>
                                        <td align="center"><?= $r['zone_name'] ?></td>
                                        <td align="center"><?= $r['engname'] ?></td>
                                        <td align="center"><a href="shipping_zone_country_delete.php?courier_id=<?= $id ?>&zone_id=<? $zone_id ?>&id=<?= $r['bid'] ?>">
                                                <img src="images/icons/delete.gif" border="0"></a>
                                        </td>


                                    </tr>
                                    <?
                                    $no++;
                                }
                            }
                            ?>
                        </table>
                    </td>
                </tr>
                <script>
                    function frmvalidate(){
                        if(document.frm1.country_id.value=="")
                        {
                            alert("Please select a country");
                            return false;
                        }
                        
                        if(document.frm1.zone_id.value=="")
                        {
                            alert("Please select a zone");
                            return false;
                        }
                        return true;
                              
                    }
                </script>
                <tr>
                    <td colspan="3">
                        <br><br>
                        <form method="post" action="<?= $_SERVER['PHP_SELF'] ?>" onsubmit="return frmvalidate()" name="frm1">
                            <div class="sub_title">Add Country To Zone</div>
                            Country
                            <select name="country_id">
                                <option value="">Please select</option>
                                <?
                                $get_country = mysql_query("select * from os_country  order by engname");

                                while ($c = mysql_fetch_assoc($get_country)) {
                                    ?>
                                    <option value="<?= $c['id'] ?>"><?= $c['engname'] ?></option>

                                <? } ?>

                            </select>
                            Zone
                            <select name="zone_id">
                                <option value="">Please Select</option>
                                <?
                                $get_zone = mysql_query("select * from os_shipping_zone where courier_id='$id' order by zone_name asc") or die(mysql_error());

                                while ($c = mysql_fetch_assoc($get_zone)) {

                                    $slt = $zone_id == $c['id'] ? "selected" : "";
                                    ?>
                                    <option value="<?= $c['id'] ?>" <?= $slt ?>><?= $c['zone_name'] ?></option>
                                <? } ?>
                            </select>
                            <input type="hidden" value="<?= $id ?>" name="courier_id">
                            
                            <input type="submit" value="Submit" name="btnsubmit">
                        </form>    
                    </td>
                </tr>


            </table></td>
    </table>
<? } require("required/footer.php"); ?>
